#include "perebor.h"
#include "is_eq.h"
#include "next_node.h"

std::valarray<double> perebor (std::valarray<double> w, std::valarray<double> ch, std::valarray<double> step, input& data,
                               double (*func)(std::valarray<double>,file_names& Fnames,input& data), file_names& Fnames) {
    unsigned size = w.size();
    unsigned k=1;
    for (unsigned i=0; i<size; i++) k *= (ch[i]+1);
    std::valarray<double> start(size), stop(size), minw(size);
    start = w-ch*step/2.0;
    stop = w+ch*step/2.0;
    minw = w;
    double minf = func(minw,Fnames,data);
    w = start;                                                       //сравниваем точку, подозрительную на минимум, с первой точкой перебора
    if (minf > func(w,Fnames,data)||(IsEq(minf,func(w,Fnames,data),1e-11))) {
        minw = w;
        minf = func(w,Fnames,data);
    }
    for (unsigned i=0; i<k-1; i++) {
        w = next_node(w,start,stop,step);
        if (minf > func(w,Fnames,data)||(IsEq(minf,func(w,Fnames,data),1e-11))) {
            minw = w;
            minf = func(w,Fnames,data);
        }
    }
    return minw;
}
